package shuiyuqing.views.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xkh.xkh.R;

import com.xkh.xkh.MyApplication;

import shuiyuqing.network.beans.HistoryIrrBRWaterDam;
import shuiyuqing.network.http.GetHistoryWaterDam;
import utils.TimeUtil;

/**
 * Created by 凯 on 2015/11/11.
 */
public class HistoryWaterAdapter extends BaseAdapter {

    // 下载完历史水情信息
    GetHistoryWaterDam dam;

    public GetHistoryWaterDam getDam() {
        return dam;
    }

    public void setDam(GetHistoryWaterDam dam) {
        this.dam = dam;
    }

    // 特意清空数据
    public void clear() {
        dam = null;
    }

    @Override
    public int getCount() {
        if (dam == null) return 0;
        return dam.getDownload().getDams().size();
    }

    @Override
    public Object getItem(int position) {
        return dam.getDownload().getDams().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HistoryIrrBRWaterDam historyIrrBRWaterDam = dam.getDownload().getDams().get(position);
        convertView = View.inflate(MyApplication.share(), R.layout.item_history, null);
        ViewHolder viewHolder = new ViewHolder();
        if (convertView.getTag() == null) {
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
            viewHolder.tv_upwater = (TextView) convertView.findViewById(R.id.id_tv_upwaterValue);
            viewHolder.tv_downwater = (TextView) convertView.findViewById(R.id.id_tv_downwaterValue);
            viewHolder.tv_flow = (TextView) convertView.findViewById(R.id.id_tv_flowValue);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (historyIrrBRWaterDam != null) {
            viewHolder.tv_time.setText(TimeUtil.timestamp2time2(historyIrrBRWaterDam.getMeaTime().getTimeInMillis()));
            viewHolder.tv_upwater.setText(historyIrrBRWaterDam.getHeightUp() + "");
            viewHolder.tv_downwater.setText(historyIrrBRWaterDam.getHeightDown() + "");
            viewHolder.tv_flow.setText(historyIrrBRWaterDam.getFluxGate() + "");
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_time;
        TextView tv_upwater;
        TextView tv_downwater;
        TextView tv_flow;
    }
}
